package com.bt7274.controller;

import com.bt7274.pojo.Notification;
import com.bt7274.service.NotificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/notifications")
public class NotificationController {

    @Autowired
    private NotificationService notificationService;

    /**
     * 获取当前用户的通知列表（分页）
     */
    @GetMapping
    public List<Notification> getNotifications(
            @RequestParam("userId") String userId,
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize
    ) {
        int offset = (page - 1) * pageSize;
        return notificationService.getUserNotifications(userId, offset, pageSize);
    }

    /**
     * 获取未读通知数量
     */
    @GetMapping("/unreadCount")
    public long getUnreadCount() {
        return notificationService.countUnreadNotifications();
    }

    /**
     * 标记某条通知为已读
     */
    @PostMapping("/read/{id}")
    public String markAsRead(@PathVariable("id") String notificationId) {
        notificationService.markAsRead(notificationId);
        return "ok";
    }

    /**
     * 标记所有通知为已读
     */
    @PostMapping("/readAll")
    public String markAllAsRead() {
        notificationService.markAllAsRead();
        return "ok";
    }
}
